<?php
/**
 *	Data Model of Role Rights.
 *	@package		tools.BugTracker
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			12.02.2006
 *	@version		0.6
 */
/**
 *	Data Model of Role ights.
 *	@package		tools.BugTracker
 *	@extends		CMF_Krypton_Model
 *	@uses			RightObject
 *	@uses			RightAction
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			12.02.2006
 *	@version		0.6
 */
final class Model_RoleRight extends CMF_Krypton_Core_Model
{
	var $_right_cache = array();
	
	/**
	 *	Constructor.
	 *	@access		public
	 *	@param		int		$roleright_id		ID of Role Right
	 *	@return		void
	 */
	public function __construct( $roleright_id = NULL )
	{
		$tablename	= "rolerights";
		$fields	= array (
			"roleright_id",
			"role_id",
			"rightobject_id",
			"rightaction_id",
			);
		$primary_key	= "roleright_id";
		$foreign_keys	= array( "role_id", "rightobject_id", "rightaction_id" );

		parent::__construct( $tablename, $fields, $primary_key, $$primary_key );
		$this->setForeignKeys ( $foreign_keys );

		$this->_rightobject	= new RightObject();
		$this->_rightaction	= new RightAction();
	}

	/**
	 *	Returns Array of Right Object IDs and Action IDs.
	 *	@access		public
	 *	@return		array
	 */
	public function getRightIDs()
	{
		$list	= $this->getAllData();
		$objectactions	= array();
		foreach( $list as $entry )
		{
			$objectactions[]	= array( $entry['rightobject_id'], $entry['rightaction_id'] );
		}
		return $objectactions;
	}
	
	/**
	 *	Returns Array of Right Object Names and Action Names.
	 *	@access		public
	 *	@return		array
	 */
	public function getRights()
	{
		$list	= $this->getAllData();
		$objectactions	= array();
		foreach( $list as $entry )
		{
			$object	= $this->_rightobject->getObject( $entry['rightobject_id'] );
			$action	= $this->_rightaction->getAction( $entry['rightaction_id'] );
			$objectactions[$entry['right_id']]	= array( $object, $action );
		}
		return $objectactions;
	}

	public function getRoleRights( $role_id )
	{
		$this->focusForeign( "role_id", $role_id );
		$list	= $this->getData( FALSE );
		return $list;
	}
	
	/**
	 *	Indicates whether Right Object and Action is set to a Role by their IDs.
	 *	@access		public
	 *	@param		int			$role_id			Role ID
	 *	@param		int			$rightobject_id		Right Object ID
	 *	@param		int			$rightaction_id		Right Action ID
	 *	@return		bool
	 */
	public function hasRightByID( $role_id, $rightobject_id, $rightaction_id )
	{
		$this->defocus();
		$this->focusForeign( "role_id", $role_id );
		$this->focusForeign( "rightobject_id", $rightobject_id );
		$this->focusForeign( "rightaction_id", $rightaction_id );
		$data	= $this->getData( TRUE );
		if( count( $data ) != 0 )
			return TRUE;
		return FALSE;
	}
	
	/**
	 *	Indicates whether Right Object and Action is set to a Role.
	 *	@access		public
	 *	@param		int			$role_id			Role ID
	 *	@param		string		$rightobject		Right Object Name
	 *	@param		string		$rightaction		Right Action Name
	 *	@return		bool
	 */
	public function hasRight( $role_id, $rightobject, $rightaction )
	{
		$object	= $this->_rightobject->getObjectID( $rightobject );
		$action	= $this->_rightaction->getActionID( $rightaction );
		if( $object && $action )
			return $this->hasRightByID( $role_id, $object, $action );
		return FALSE;
	}
}
?>
